(function($, window, undefined) {
    $.EtTree = $.EtTree || {};
	$.EtTree.list = {};
	
	$.extend($.EtTree, {
        "version": "1.0.0",
		//定义外部扩展方法接口
        "extend": function(methods) {
            $.extend($.fn.EtTree, methods);
			
			if (!this.no_legacy_api) {
				$.fn.extend(methods);
			}
        },
		"randomString" : function(length) {
			var chars = 'abcdefghiklmnopqrstuvwxyz'.split('');
			 
			if (! length) {
				 length = Math.floor(Math.random() * chars.length);
			 }
			 
			var str = '';
			 for (var i = 0; i < length; i++) {
				 str += chars[Math.floor(Math.random() * chars.length)];
			 }
			 return str;
		 }
    });
	
	//常用扩展方法
	$.EtTree.extend({
		
    });
	
	
	$.fn.EtTree = function(options) {
        var _this = this;

		//合并参数
        var config = $.extend({},$.fn.EtTree.defaults, options);
        
		_this.config = config;
		
		//初始化
		init(_this);
		
		if(_this.config.objId){
		}else{
			_this.config.objId = $.EtTree.randomString(8);
		}
		
		$.EtTree.list[_this.config.objId] = _this;
				
		return _this;
    };
	
	$.fn.EtTree.defaults = {
		nowLevel: 1, //数据获取地址
		nowId: 0
    };
	
	var init = function(_this){
		var _that = _this;
		
		nodeOpen(_that);
		bindEvent(_that);
	};
	var nodeOpen = function(_this){
		var _that = _this;
		
		//显示第一级
		if(_that.config.nowLevel==1 && _that.config.nowId === 0){
			_that.find('tr').hide();
			_that.find('tr .name').prepend('<span class="unfold">&nbsp;</span>');
			_that.find('.level_'+_that.config.nowLevel).show();
			
			//调整位置
			_that.find('.unfold').each(function(index, element) {
				//没有下级的设置为nochild
				if($(this).parent('td').parent('tr').attr('isleaf')==2){
					$(this).removeClass('unfold').addClass('nochild');
				}
				
				//设置边距
				var mgl = parseInt($(this).parent('td').parent('tr').attr('level'))*30-30;
				$(this).css('margin-left',mgl+'px')
			});
		}else{
			//显示下一级
			_that.find('#area_'+_that.config.nowId).nextAll().each(function(index, element) {
				if($(this).attr('level')==(parseInt(_that.config.nowLevel)+1)){
					$(this).show();
				}
				if($(this).attr('level')==_that.config.nowLevel){
					return false;
				}
			});
		}
	};
	var nodeClose = function(_this){
		var _that = _this;
		
		_that.find('#area_'+_that.config.nowId).nextAll().each(function(index, element) {
			if($(this).attr('level')>_that.config.nowLevel){
				if($(this).find('.name>span').is('.fold')){
					$(this).find('.name>span').removeClass('fold').addClass('unfold');
				}						
				$(this).hide();
			}else{
				return false;
			}
		});
	};
	var bindEvent = function(_this){
		var _that = _this;
		
		_that.find('.unfold').live('click',function(){
			$(this).removeClass('unfold').addClass('fold');
			_that.config.nowLevel = $(this).parent('td').parent('tr').attr('level');
			_that.config.nowId = $(this).parent('td').parent('tr').attr('dataid');
			
			nodeOpen(_that);
		});
		
		_that.find('.fold').live('click',function(){
			$(this).removeClass('fold').addClass('unfold');
			_that.config.nowLevel = $(this).parent('td').parent('tr').attr('level');
			_that.config.nowId = $(this).parent('td').parent('tr').attr('dataid');
			
			nodeClose(_that);
		});
	};
})(jQuery, window);